﻿using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Hql.Ast;
using NHibernate.Linq;
using NHibernate.Linq.Functions;
using NHibernate.Linq.Visitors;

namespace NHDay_Linq.ProviderExtensions
{
	/// <summary>
	/// Equals generator used for the generic runtime implementation
	/// </summary>
	public class EqualsObjectGenerator : BaseHqlGeneratorForMethod
	{
		public EqualsObjectGenerator()
		{
			SupportedMethods = new[]
			                   	{
			                   		ReflectionHelper.GetMethodDefinition<object>(x => x.Equals(null))
			                   	};
		}

		public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, 
			ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
		{
			return treeBuilder.Equality(
				visitor.Visit(targetObject).AsExpression(),
				visitor.Visit(arguments[0]).AsExpression());
		}
	}
}